/*
 * BlackjackGUI.java
 *
 * Created on September 2, 2007, 5:35 PM
 */

package blackjacksim;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import javax.swing.UIManager;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author  Eric Drucker
 */
public class BlackjackGUI extends javax.swing.JFrame
{

	BlackjackGame game;
	BufferedImage img;
        static JFrame self;
        
        ArrayList<Card> playerHand;
        ArrayList<Card> computerHand;
        int numCards = 0;  
       	
	/** Creates new form BlackjackGUI */
	public BlackjackGUI()
	{
		
		initComponents();
		
		game = new BlackjackGame();
                
                hitButton.setEnabled(false);
		standButton.setEnabled(false);           
                  
		updateDisplay();
	}

	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        resultDialog = new javax.swing.JDialog();
        betTextField = new javax.swing.JTextField();
        betButton = new javax.swing.JButton();
        standButton = new javax.swing.JButton();
        hitButton = new javax.swing.JButton();
        moneyLabel = new javax.swing.JLabel();
        betLabel = new javax.swing.JLabel();
        gamePanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        resultDialog.setTitle("End of round!");
        resultDialog.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        resultDialog.setLocationByPlatform(true);
        resultDialog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                resultDialogWindowClosing(evt);
            }
        });
        resultDialog.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                resultDialogComponentMoved(evt);
            }
        });

        javax.swing.GroupLayout resultDialogLayout = new javax.swing.GroupLayout(resultDialog.getContentPane());
        resultDialog.getContentPane().setLayout(resultDialogLayout);
        resultDialogLayout.setHorizontalGroup(
            resultDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        resultDialogLayout.setVerticalGroup(
            resultDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Blackjack");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        betTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                betTextFieldActionPerformed(evt);
            }
        });

        betButton.setText("Bet");
        betButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                betButtonActionPerformed(evt);
            }
        });

        standButton.setText("Stand");
        standButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                standButtonActionPerformed(evt);
            }
        });

        hitButton.setText("Hit");
        hitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hitButtonActionPerformed(evt);
            }
        });

        moneyLabel.setText("Cash $1234");

        betLabel.setText("Bet $1234");

        gamePanel.setBackground(new java.awt.Color(0, 153, 0));
        javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
        gamePanel.setLayout(gamePanelLayout);
        gamePanelLayout.setHorizontalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        gamePanelLayout.setVerticalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 308, Short.MAX_VALUE)
        );

        jMenu1.setText("File");
        jMenuItem1.setText("Reset");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(betButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(betTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(moneyLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(betLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                .addGap(9, 9, 9)
                .addComponent(hitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(standButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(gamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(gamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(betButton)
                    .addComponent(betTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(standButton)
                    .addComponent(hitButton)
                    .addComponent(moneyLabel)
                    .addComponent(betLabel))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resultDialogWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_resultDialogWindowClosing
// TODO add your handling code here:
        
    }//GEN-LAST:event_resultDialogWindowClosing

    private void resultDialogComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_resultDialogComponentMoved
// TODO add your handling code here:
        
    }//GEN-LAST:event_resultDialogComponentMoved

     /**
	public void paintComponents(Graphics g)
	{
		if(game.getGameInProgress() == true)
		{
			betButton.setEnabled(false);
			standButton.setEnabled(true);
			hitButton.setEnabled(true);
			
			g.drawImage(img, 10, 55, gamePanel);
			g.drawImage(img, 10, 255, gamePanel);
		
		}
		else
		{
			betButton.setEnabled(true);
			standButton.setEnabled(false);
			hitButton.setEnabled(false);
		}
		
		moneyLabel.setText("Cash $" + game.getMoney());
		betLabel.setText("Bet $" + game.getBetAmount());
		
		
	}
      **/
	
	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItem1ActionPerformed
	{//GEN-HEADEREND:event_jMenuItem1ActionPerformed
		game = new BlackjackGame();
		
                betButton.setEnabled(true);
                hitButton.setEnabled(false);
		standButton.setEnabled(false); 
                
		updateDisplay();
	}//GEN-LAST:event_jMenuItem1ActionPerformed

	private void standButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_standButtonActionPerformed
	{//GEN-HEADEREND:event_standButtonActionPerformed
		game.stand();
		
		updateDisplay();
	}//GEN-LAST:event_standButtonActionPerformed

	private void hitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_hitButtonActionPerformed
	{//GEN-HEADEREND:event_hitButtonActionPerformed
		game.hit();
		
		updateDisplay();
	}//GEN-LAST:event_hitButtonActionPerformed

	private void betButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_betButtonActionPerformed
	{//GEN-HEADEREND:event_betButtonActionPerformed
		int betAmount;
                
                if(betTextField.getText() != null && !betTextField.getText().equals(""))              
                {
                    betAmount = Integer.parseInt(betTextField.getText());
                    if(betAmount > 0)
                    {
                    game.bet(betAmount);
                    System.out.println("Entered bet amount:" + betAmount);

                    betButton.setEnabled(false);
                    hitButton.setEnabled(true);
                    standButton.setEnabled(true); 

                    updateDisplay();
                    }
                }
	}//GEN-LAST:event_betButtonActionPerformed

	private void betTextFieldActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_betTextFieldActionPerformed
	{//GEN-HEADEREND:event_betTextFieldActionPerformed
// TODO add your handling code here:
	}//GEN-LAST:event_betTextFieldActionPerformed
	
        private void updateDisplay()
        {
                moneyLabel.setText("Cash $" + game.getMoney());
		betLabel.setText("Bet $" + game.getBetAmount());
                
               this.repaint();
                               
                if(game.getFinished())
                {
                    /*
                   try
                   {
                        Thread.sleep(3000);
                   }
                   catch(InterruptedException er)
                   {
                       System.out.println("Interrupted!");
                   }
                     */
                    JOptionPane.showMessageDialog(self, game.getResults());
                    game.nextGame();
                    
                    betButton.setEnabled(true);
                    hitButton.setEnabled(false);
                    standButton.setEnabled(false);
                    
                    updateDisplay();
                }
        }
         public void paint(Graphics g) 
        {
            super.paint(g);

            ArrayList<Card> playerHand = game.getPlayerHand();
            ArrayList<Card> computerHand = game.getComputerHand();
            
            numCards = 0;
            
            for(Card c: playerHand)
            {
                String imgString = c.getImage();
                System.out.println(imgString);

                try 
                {
                    img = ImageIO.read(new File(imgString));
                } 
                catch (IOException e) 
                {
                    System.out.println("Caught IOException when trying to read image file.");
                }

                g.drawImage(img, numCards+=30, 240, null);
                
            }
            
            numCards = 0;
                                 
            for(Card c: computerHand)
            {               
                {
                String imgString = c.getImage();
                System.out.println(imgString);

                try 
                {   
                    if(computerHand.size()==2)
                        if(computerHand.indexOf(c)==1)
                            imgString = "./cards/blank.gif";
                    img = ImageIO.read(new File(imgString));
                } 
                catch (IOException e) 
                {
                    System.out.println("Caught IOException when trying to read image file.");
                }
                g.drawImage(img, numCards+=30, 60, null);
                }
            }
         }
            
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[])
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
                            self = new BlackjackGUI();
                            self.setVisible(true);
			}
		});
	}
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton betButton;
    private javax.swing.JLabel betLabel;
    private javax.swing.JTextField betTextField;
    private javax.swing.JPanel gamePanel;
    private javax.swing.JButton hitButton;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel moneyLabel;
    private javax.swing.JDialog resultDialog;
    private javax.swing.JButton standButton;
    // End of variables declaration//GEN-END:variables
}
